﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Data.SqlClient;
using Microsoft.ApplicationBlocks.Data;
using System.Data;

namespace DanhmucThietbi
{
    class PhanLoaiInfo
    {
        private string _MA_PLOAI;

        public string MA_PLOAI
        {
            get { return _MA_PLOAI; }
            set { _MA_PLOAI = value; }
        }

        private string _TEN_PLOAI;

        public string TEN_PLOAI
        {
            get { return _TEN_PLOAI; }
            set { _TEN_PLOAI = value; }
        }

        private string _NGUOI_TAO;

        public string NGUOI_TAO
        {
            get
            {
                if (_NGUOI_TAO == null) _NGUOI_TAO = "ADMIN";
                return _NGUOI_TAO;
            }
            set { _NGUOI_TAO = value; }
        }

        private DateTime _NGAY_TAO;

        public DateTime NGAY_TAO
        {
            get { return _NGAY_TAO; }
            set { _NGAY_TAO = value; }
        }

        private string _NGUOI_SUA;

        public string NGUOI_SUA
        {
            get
            {
                if (_NGUOI_SUA == null) _NGUOI_SUA = "ADMIN";
                return _NGUOI_SUA;
            }
            set { _NGUOI_SUA = value; }
        }

        private DateTime _NGAY_SUA;

        public DateTime NGAY_SUA
        {
            get { return _NGAY_SUA; }
            set { _NGAY_SUA = value; }
        }

        private bool _IS_DELETE;

        public bool IS_DELETE
        {
            get { return _IS_DELETE; }
            set { _IS_DELETE = value; }
        }
    }

    class CtrPhanLoai
    {
        string myCnn = "";
        public CtrPhanLoai()
        {
            myCnn=ConfigurationManager.ConnectionStrings["myConnectionString"].ToString();
        }

        public List<PhanLoaiInfo> GetAll()
        {
            List<PhanLoaiInfo> returnList = new List<PhanLoaiInfo>();
            SqlDataReader ObjRead = SqlHelper.ExecuteReader(myCnn, CommandType.StoredProcedure, "DEV_SP_PLOAI_GetAllItem");
            while (ObjRead.Read())
            {
                PhanLoaiInfo ObjPLoai = new PhanLoaiInfo();
                ObjPLoai.MA_PLOAI = ObjRead["MA_PLOAI"].ToString();
                ObjPLoai.TEN_PLOAI = ObjRead["TEN_PLOAI"].ToString();
                ObjPLoai.NGUOI_TAO = ObjRead["NGUOI_TAO"].ToString();
                returnList.Add(ObjPLoai);
            }
            return returnList;
        }

        public PhanLoaiInfo GetByID(string MaPLoai)
        {
            SqlDataReader ObjRead = SqlHelper.ExecuteReader(myCnn, CommandType.StoredProcedure, "DEV_SP_PLOAI_GetByID", new SqlParameter("@MA_PLOAI", MaPLoai));
            PhanLoaiInfo ObjPLoai = null;
            while (ObjRead.Read())
            {
                ObjPLoai = new PhanLoaiInfo();
                ObjPLoai.MA_PLOAI = ObjRead["MA_PLOAI"].ToString();
                ObjPLoai.TEN_PLOAI = ObjRead["TEN_PLOAI"].ToString();
                ObjPLoai.NGUOI_TAO = ObjRead["NGUOI_TAO"].ToString();
                ObjPLoai.NGAY_TAO = (DateTime)ObjRead["NGAY_TAO"];
                ObjPLoai.NGUOI_SUA = ObjRead["NGUOI_SUA"].ToString();
                ObjPLoai.NGAY_SUA = (DateTime)ObjRead["NGAY_SUA"];
            }
            return ObjPLoai;
        }

        public void InsertItem(PhanLoaiInfo Item)
        {
            SqlParameter[] Para = new SqlParameter[3];
            Para[0] = new SqlParameter("MA_PLOAI", Item.MA_PLOAI);
            Para[1] = new SqlParameter("TEN_PLOAI", Item.TEN_PLOAI);
            Para[2] = new SqlParameter("NGUOI_TAO", Item.NGUOI_TAO);
            try
            {
                SqlHelper.ExecuteNonQuery(myCnn, CommandType.StoredProcedure, "DEV_SP_PLOAI_Insert", Para);
            }
            catch (Exception)
            {
                throw new System.ArgumentException(string.Format("Mã phân loại {0} đã tồn tại, vui lòng chọn mà khác", Item.MA_PLOAI));
            }
        }

        public void UpdateItem(PhanLoaiInfo Item)
        {
            SqlParameter[] Para = new SqlParameter[3];
            Para[0] = new SqlParameter("MA_PLOAI", Item.MA_PLOAI);
            Para[1] = new SqlParameter("TEN_PLOAI", Item.TEN_PLOAI);
            Para[2] = new SqlParameter("NGUOI_SUA", Item.NGUOI_SUA);
            try
            {
                SqlHelper.ExecuteNonQuery(myCnn, CommandType.StoredProcedure, "DEV_SP_PLOAI_Update", Para);
            }
            catch (Exception)
            {

                throw;
            }

        }

        public void DeleteItem(PhanLoaiInfo Item)
        {
            SqlParameter[] Para = new SqlParameter[2];
            Para[0] = new SqlParameter("MA_PLOAI", Item.MA_PLOAI);
            Para[1] = new SqlParameter("NGUOI_SUA", Item.NGUOI_SUA);
            SqlHelper.ExecuteNonQuery(myCnn, CommandType.StoredProcedure, "DEV_SP_PLOAI_Delete", Para);
        }

    }
}
